<?php
namespace app\hesuan\controller;
use think\Controller;
use think\Request;
use think\Db;
use DateTime;
class Student extends Controller
{


    public function login()
    {
        return $this->fetch();
    }   




    public function index()
    {
        if(session('stu_num')==null){
            return "请先登录！";
        }

        $init_date = "2022-04-18"; 
        $today = date("Y-m-d H:i:s");

        $datetime_init = new DateTime($init_date);
        $datetime_today = new DateTime($today);
        $days = $datetime_init->diff($datetime_today)->days;
        $week_num = floor($days / 7) +1  ; //当前日期处于第几周
        $day_delta1 = $days % 7; //相对于本周一偏离的日期
        $day_delta2 = 6 - $day_delta1; //相对于本周日偏离的日期
        $day_delta3 = $day_delta1+7; //相对于上周一偏离的日期
        $day_delta4 = $day_delta1+1; //相对于上周日偏离的日期
        $day_delta5 = $day_delta3+7; //相对于上上周一偏离的日期

        $thisweek_start = date("Y-m-d", strtotime("-".$day_delta1." day"));
        $thisweek_end = date("Y-m-d", strtotime($day_delta2." day"));
        $lastweek_start = date("Y-m-d", strtotime("-".$day_delta3." day"));
        $lastweek_end = date("Y-m-d", strtotime("-".$day_delta4." day"));
        $lastlastweek_start = date("Y-m-d", strtotime("-".$day_delta5." day"));
        $deadline = new DateTime($thisweek_end);

        if($datetime_today >= $deadline){
            return "本周核酸检测统计已截止！";
        }


        $this->assign('thisweek_start', $thisweek_start);
        $this->assign('thisweek_end', $thisweek_end);
        $this->assign('lastweek_start', $lastweek_start);
        $this->assign('lastweek_end', $lastweek_end);
        $this->assign('lastlastweek_start', $lastlastweek_start);
        return $this->fetch();
    }


    public function home()
    {
        if(session('stu_num')==null){
            return "请先登录！";
        }


        $request = Request::instance();
        $id = $request->param('id');
        $msg='';


        $init_date = "2022-04-18"; 
        $today = date("Y-m-d");
        $datetime_init = new DateTime($init_date);
        $datetime_today = new DateTime($today);
        $days = $datetime_init->diff($datetime_today)->days;
        $week_num = floor($days / 7) +1  ; //当前日期处于第几周
        $day_delta1 = $days % 7; //相对于本周一偏离的日期
        $day_delta2 = 6 - $day_delta1; //相对于本周日偏离的日期
        $day_delta3 = $day_delta1+7; //相对于上周一偏离的日期
        $day_delta4 = $day_delta1+1; //相对于上周日偏离的日期
        $day_delta5 = $day_delta3+7; //相对于上上周一偏离的日期

        $thisweek_start = date("Y-m-d", strtotime("-".$day_delta1." day"));
        $thisweek_end = date("Y-m-d", strtotime($day_delta2." day"));
        $lastweek_start = date("Y-m-d", strtotime("-".$day_delta3." day"));
        $lastweek_end = date("Y-m-d", strtotime("-".$day_delta4." day"));
        $lastlastweek_start = date("Y-m-d", strtotime("-".$day_delta5." day"));



        switch($id){
            case 1:
            case 2:
                $msg = "同学，你本周(".$thisweek_start."到".$thisweek_end.")不必做核酸，当然你如果非得做，也没人拦着。";
                break;
            case 3:
            case 4:
                $msg = "同学，你本周(".$thisweek_start."到".$thisweek_end.")不必做核酸，当然你如果非得做，也没人拦着。";
                break;
            case 5:
                $msg = "同学，你本周(".$thisweek_start."到".$thisweek_end.")必须做核酸！";
                break;
            }
        $this->assign('msg', $msg);
        $this->assign('id', $id);

        return $this->fetch();
    }

    public function save(){
        if(session('stu_num')==null){
            return "请先登录！";
        }

        $request = Request::instance();
        $hesuan = $request->param('hesuan');
        $place = $request->param('place');
        $reason = $request->param('reason');
        $status = $request->param('type');
        
        // echo $hesuan;
        // echo $type;
        
        $init_date = "2022-04-18"; 
        $today = date("Y-m-d H:i:s");
        $datetime_init = new DateTime($init_date);
        $datetime_today = new DateTime($today);
        $days = $datetime_init->diff($datetime_today)->days;
        $week_num = floor($days / 7) +1  ; //当前日期处于第几周
        $day_delta1 = $days % 7; //相对于本周一偏离的日期
        $day_delta2 = 6 - $day_delta1; //相对于本周日偏离的日期
        $thisweek_start = date("Y-m-d", strtotime("-".$day_delta1." day"));
        $thisweek_end = date("Y-m-d", strtotime($day_delta2." day"));
        $where = [
            'created_at'=>['between',[$thisweek_start." 00:00:00", $thisweek_end." 23:59:59"]],
            'num'=>session('stu_num')
        ];


        Db::table('hesuan_info')->where($where)->update(['is_newest' => 0]);

        $data = ['num'=>session('stu_num'), 'name'=>session('stu_name'), 'is_tested'=>$hesuan, 'place'=>$place, 'reason'=>$reason, 'status'=>$status, 'is_newest'=>1];
        Db::table('hesuan_info')->insert($data);

        return '提交成功！';
    }



    public function dologin(){
        if($this->request->isPost()){

            $username = $this->request->post('username/s','','trim');
            $password = $this->request->post('password/s','','trim');
            $count = Db::table('student_info')->where('num',$username)->where('tel',$password)->count();
            // echo $count;
            // var_dump( Db::table('student_info')->where('tel','=',$password)->select());
            // return 'haha';
            if($count!=1){
                return '用户名或密码错误！';
            }
            else{
                session('stu_num',$username);
                session('stu_name',Db::table('student_info')->where('num',$username)->column('name')[0]);
                return $this->fetch();
            }
        }
    }

    public function logout(){
        session(null);
        return "退出登录";
    }
}
